package com.puntodonna.fogliocassa.client.app.presenter.header;

import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.google.inject.Inject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.puntodonna.fogliocassa.client.app.presenter.main.MainPresenter;
import com.puntodonna.fogliocassa.client.place.NameTokens;
import com.puntodonna.fogliocassa.shared.UserObj;

public class HeaderPresenter extends Presenter<HeaderPresenter.MyView, HeaderPresenter.MyProxy> {

	public interface MyView extends View {
		public void setUserInfo(String userInfo);
		public Label getLogoutLabel();
		public HorizontalPanel getInfoPanel();
	}

	protected UserObj userObj;
	
	@Inject
	protected PlaceManager placeManager;

	@ProxyCodeSplit
	public interface MyProxy extends Proxy<HeaderPresenter> {
	}

	@Inject
	public HeaderPresenter(final EventBus eventBus, final MyView view, final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MainPresenter.TYPE_HEADER_AREA, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		
		getView().getLogoutLabel().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				getPlaceManager().revealPlace(new PlaceRequest(NameTokens.getLogin()), false);
			}
		});
	}
	
	public PlaceManager getPlaceManager() {
		return placeManager;
	}

	public void setUserObject(UserObj userObj) {
		this.userObj = userObj;
		getView().setUserInfo("Logged as: " + getUserObj().getName());
	}

	public UserObj getUserObj() {
		return userObj;
	}
}
